package com.common.app.utls.imagepreviewer

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Spanned
import android.text.style.ForegroundColorSpan
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import com.common.app.R
import com.common.app.databinding.ActivityImagePreviewerBinding
import com.common.base.adapter.BaseFragmentPageAdapter
import com.common.base.app.activity.BaseVBActivity
import com.common.base.utils.StatusBarUtils
import com.common.base.utils.span.TextSpanHelper

/**
 * 作者：sosou
 * <p>
 * 版本：1.0
 * <p>
 * 创建日期：2022/9/1
 * <p>
 * 描述：图片预览界面
 * <p>
 * 修订历史：
 */
class ImagePreviewerActivity : BaseVBActivity<ActivityImagePreviewerBinding>(){

    companion object {
        var TAG = ImagePreviewerActivity::class.java.simpleName
        const val IMG_URLS = "imgUrls"
        const val POSITION = "position"

        fun startActivity(context: Context, url: String) {
            val urls = ArrayList<String>(1)
            urls.add(url)
            startActivity(context, urls, 0)
        }

        fun startActivity(context: Context, urls: ArrayList<String>, currentPosition: Int) {
            val intent = Intent(context, ImagePreviewerActivity::class.java)
            intent.putStringArrayListExtra(IMG_URLS, urls)
            intent.putExtra(POSITION, currentPosition)
            context.startActivity(intent)
        }
    }

    private var mImageUrls: List<String> = ArrayList()
    private val mFragments = ArrayList<Fragment>()
    private var mCurrentPosition: Int = 0

    override fun getViewBinding(): ActivityImagePreviewerBinding = ActivityImagePreviewerBinding.inflate(layoutInflater)

    override fun initViews() {
        super.initViews()
        StatusBarUtils.setStatusBarHeight(mViewBinding.statusView)
        StatusBarUtils.setStatusBarTranslucent(this)

        mImageUrls = intent.getStringArrayListExtra(IMG_URLS) ?: arrayListOf()
        val position = intent.getIntExtra(POSITION, 0)
        mCurrentPosition = position

        if (mImageUrls.isEmpty()) {
            return
        }

        for (i in mImageUrls.indices) {
            val url = mImageUrls[i]
            val imageFragment = ImagePreviewerItemFragment()

            val bundle = Bundle()
            bundle.putString(ImagePreviewerItemFragment.IMG_URL, url)
            imageFragment.arguments = bundle

            mFragments.add(imageFragment)//添加到fragment集合中
        }

        mViewBinding.vpImg.apply {
            offscreenPageLimit = mImageUrls.size
            adapter = BaseFragmentPageAdapter(supportFragmentManager, mFragments, arrayListOf())
            addOnPageChangeListener(object: ViewPager.SimpleOnPageChangeListener(){
                override fun onPageSelected(position: Int) {
                    mCurrentPosition = position
                    setIndicator(mCurrentPosition)
                }
            })
            currentItem = mCurrentPosition// 设置当前所在的位置
        }
        setIndicator(mCurrentPosition)//设置当前位置指示
    }

    override fun initEvents() {
        super.initEvents()
        mViewBinding.ivBack.setOnClickListener {
            finish()
        }
    }

    //设置当前的指示
    private fun setIndicator(position: Int) {
        val text = (position + 1).toString() + "/" + mImageUrls.size
        val spannable = TextSpanHelper.getAbsoluteSizeSpanBuilder(text, 32, 0, text.indexOf("/"))
        spannable.setSpan(ForegroundColorSpan(resources.getColor(R.color.colorPrimaryDark)),0, text.indexOf("/"), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
        mViewBinding.tvIndicator.text = spannable
    }
}